package com.vegetable.modules.service.baseData.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.Query;
import com.vegetable.modules.entity.baseData.Products;
import com.vegetable.modules.entity.quotation.QuotationArea;
import com.vegetable.modules.mapper.baseData.ProductsMapper;
import com.vegetable.modules.service.baseData.IProductsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 产地商品 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-04
 */
@Slf4j
@Service
@AllArgsConstructor
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements IProductsService {

    /**
     * 分页查询产地商品
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String standardName = (String)params.get("standardName");
        String commonName = (String)params.get("commonName");
        String commonCode = (String)params.get("commonCode");
        String isEnabled = (String) params.get("isEnabled");
        Object regionId = params.get("regionId");
        String tags = (String)params.get("tags");
        Integer tags_int = null;
        if (StringUtils.isNotBlank(tags)){
            tags_int = Integer.valueOf(tags);
        }
        Object categoryId = params.get("categoryId");
        if(StringUtils.isBlank(isEnabled)){
            isEnabled = "0";
        }
        if("2".equals(isEnabled)){
            isEnabled = "";
        }
        IPage<Products> page = this.page(
                new Query<Products>().getPage(params),
                new QueryWrapper<Products>()
                        .like(StringUtils.isNotBlank(standardName),"standard_name", standardName)
                        .like(StringUtils.isNotBlank(commonName),"common_name", commonName)
                        .like(StringUtils.isNotBlank(commonCode),"common_code", commonCode)
                        .eq(ObjectUtils.isNotEmpty(tags_int),"tags", tags_int)
                        .eq(ObjectUtils.isNotEmpty(regionId),"region_id", regionId)
                        .eq(ObjectUtils.isNotNull(categoryId),"category_id", categoryId)
                        .eq(StringUtils.isNotBlank(isEnabled),"is_enabled", isEnabled)
                        .last("order BY CASE     WHEN common_name = '"+standardName+"' THEN 1    " +
                                "WHEN common_name LIKE '"+standardName+"%' THEN 2    " +
                                "WHEN common_name LIKE '%"+standardName+"' THEN 3    " +
                                "WHEN common_name LIKE '%"+standardName+"%' THEN 4    ELSE 5    " +
                                "END, common_name, standard_weight")
        );

        return new PageUtils(page);
    }

    /**
     * 入库查询商品使用
     * @param params null
     * @return PageUtils
     * @author wrj
     * @since 2024/4/17 下午10:54
     */
    @Override
    public PageUtils listProducts(Map<String, Object> params) {
        IPage<Products> page = baseMapper.listProducts(
                new Query<Products>().getPage(params),params
        );

        return new PageUtils(page);
    }

    @Override
    public Products queryByCommonCode(String commonCode) {
        QueryWrapper<Products> logQuery = new QueryWrapper<>();
        logQuery.eq("common_code", commonCode);
        logQuery.eq("is_enabled", 0);
        return this.getOne(new QueryWrapper<Products>().eq("common_code", commonCode));
    }

    @Override
    public List<Products> queryList(Products po) {
        QueryWrapper<Products> logQuery = new QueryWrapper<Products>()
                .eq(Objects.nonNull(po.getTags()),"tags", po.getTags())
                .eq(StringUtils.isNotBlank(po.getPlaceOrigin()), "place_origin", po.getPlaceOrigin())
                .eq(StringUtils.isNotBlank(po.getCategory()),"category", po.getCategory())
                .eq(Objects.nonNull(po.getCategoryId()),"category_id", po.getCategoryId())
                .orderByDesc("id");
        return this.baseMapper.selectList(logQuery);
    }

    @Override
    public List<Products> queryListByIds(List<Long> idList) {
        QueryWrapper<Products> logQuery = new QueryWrapper<>();
        logQuery.in("id", idList);
        logQuery.eq("is_enabled", 0);
        return this.baseMapper.selectList(logQuery);
    }

    @Override
    public String createCommonNo(QuotationArea area, Integer tags) {
        Products po = new Products();
        po.setTags(tags);
        po.setPlaceOrigin(area.getAreaName());
        List<Products> list = queryList(po);
        Integer num = CollectionUtil.isEmpty(list) ?  1 : list.get(0).getId().intValue() + 1;
        // 商品编码组成：产地标识 + 商品分类 + 顺序数字
        String commonCode = area.getAreaSign() + po.getTags() + StrUtil.padPre(num.toString(), 5, '0');
        return commonCode;
    }

    @Override
    public String getNextCode() {
        return this.baseMapper.getNextCode();
    }
}
